﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using System.Data;
namespace DbCommon
{
    public class Utils
    {
        /// <summary>
        /// 生成strLong个str字符串
        /// </summary>
        /// <param name="strLong">生成字符串的个数</param>
        /// <param name="str">字符串</param>
        /// <returns>返回strLong个str字符串</returns>
        public static string stringOfChar(int strLong, char ctr)
        {
            string returnStr = string.Empty;
            for (int i = 0; i < strLong; i++)
            {
                returnStr += ctr;

            }
            return returnStr;
        }
        /// <summary>
        /// 添加cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="content">要添加的内容</param>
        public static void writeToCookie(string cookieName, string content)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Value = content;
            HttpContext.Current.Response.AppendCookie(cookie);

        }
        /// <summary>
        /// 读取cookie的值
        /// </summary>
        /// <param name="cookieName">cookie的名字</param>
        /// <returns>返回cookie值</returns>
        public static string GetCookie(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null)
                return HttpContext.Current.Request.Cookies[cookieName].Value.ToString();
            return "";
        }
        /// <summary>
        /// 分割一条语句，获得一个语句数组
        /// </summary>
        /// <param name="oldStr">要分割的语句</param>
        /// <param name="splitor">分隔符</param>
        /// <returns>返回语句数组</returns>
        public static string[] SplitString(string oldStr, char splitor)
        {
            string[] strArray = oldStr.Split(splitor);
            return strArray;
        }
        #region 获得配置文件节点XML文件的绝对路径
        public static string GetXmlMapPath(string xmlName)
        {
            return GetMapPath(ConfigurationManager.AppSettings[xmlName].ToString());
        }
        #endregion
        #region 获得当前绝对路径
        /// <summary>
        /// 获得当前绝对路径
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(string strPath)
        {
            if (strPath.ToLower().StartsWith("http://"))
            {
                return strPath;
            }
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
        #endregion
    }
}
